<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)"/>
      </template>
    </BasicTable>
    <MchDrawer @register="registerMchDrawer" @success="reload" />
  </div>
</template>
<script lang="ts" name="PayWayList" setup>
import {BasicTable, TableAction} from '/@/components/Table';
import {useDrawer} from '/@/components/Drawer';
import MchDrawer from './PayWayDrawer.vue';
import {useGlobSetting} from '/@/hooks/setting';
import {columns, searchFormSchema} from './payWay.data';
import {deletePayWay, listPayWay} from './payWay.api';
import {useListPage} from '/@/hooks/system/useListPage';
import {useMessage} from '/@/hooks/web/useMessage';
import {useRouter} from "vue-router"

const { createMessage } = useMessage();
const router = useRouter()

const glob = useGlobSetting();
const [registerMchDrawer, { openDrawer: openMchDrawer }] = useDrawer();
// 列表页面公共参数、方法
const { prefixCls, tableContext } = useListPage({
  designScope: 'PayWayList',
  tableProps: {
    title: '支付方式列表',
    api: listPayWay,
    columns: columns,
    bordered: false,
    actionColumn: {
      width: 100,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
    formConfig: {
      schemas: searchFormSchema,
      fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']],
    },
  }
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

/**
 * 编辑
 */
function handleEdit(record: Recordable) {
  openMchDrawer(true, {
    record,
    isUpdate: true,
  });
}


/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      // ifShow: record.sendStatus == 0
    },
    {
      label: '删除',
      color: 'error',
      ifShow: record.sendStatus != 1,
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    }
  ]
}

/**
 * 删除
 */
const handleDelete = async (record) => {
  await deletePayWay({ wayCode: record.wayCode }, () => {
    reload()
    createMessage.success("删除成功")
  })
  
}

/**
 * 新增
 */
 function handleCreate() {
  openMchDrawer(true, {
    isUpdate: false,
  });

  
}

</script>
